home *** CD-ROM | disk | FTP | other *** search
- Path: erich.triumf.ca!bennett
- From: bennett@erich.triumf.ca (P.Bennett)
- Newsgroups: comp.lang.c
- Subject: Re: Help:what is wrong this code?
- Date: 10 Apr 1996 07:39 PST
- Organization: TRIUMF: Tri-University Meson Facility
- Distribution: world
- Message-ID: <10APR199607391465@erich.triumf.ca>
- References: <4k3p3q$n76@brahms.udel.edu> <4k4hi3$5hm@sparcserver.lrz-muenchen.de> <Pine.ULT.3.92.960409210416.383A-100000@henson.cc.wwu.edu>
- NNTP-Posting-Host: ftp.triumf.ca
- News-Software: VAX/VMS VNEWS 1.50
-
- In article <Pine.ULT.3.92.960409210416.383A-100000@henson.cc.wwu.edu>, Norma Mathews <n9341884@henson.cc.wwu.edu> writes...
- >On 6 Apr 1996, Kurt Watzka wrote:
- >> yuehong@brahms.udel.edu (Yue-hong Zheng) writes:
- >> >Why it give me 0.0000?
- >>
- >> >#include <stdio.h>
- >> >main () {
- >> >double a=9.008;
- >> >printf("%f\n",sqrt(a));
- >> >return 0;
- >> >}
- >your printf specifer needs to be "%lf", not "%f", since a is a double
-
- No!
-
- %f is correct for _printf()_, since with variable argument lists, floats are
- promoted to doubles. printf() can never see a float, so it does not need any
- way to distinguish between float and double. (not so with scanf(), which does
- use %f and %lf.)
-
- (anyway, we aren't printing a, we are printing sqrt(a), so the type of a is
- irrelevant.)
-
- The problem apparently is that the original poster failed to #include <math.h>,
- so the compiler will assume sqrt() returns an int.
-
- Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
- Internet: bennett@triumf.ca | of one another only when one can be
- Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
- TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
- GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
- or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
-
-
-
-